﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LibService.DTO;

namespace LibService.DAO
{
    public class DuLieuDAO
    {
        #region 1. Lay Danh Sach Thong Tin Du Lieu
        public static List<DuLieuDTO> layDanhSachDuLieu()
        {
            List<DuLieuDTO> lst = new List<DuLieuDTO>();
            try
            {
                SQLConnect cnn = new SQLConnect();
                DataTable dt = new DataTable();
                dt = cnn.ExecuteQuery("sp_LayThongTinDuLieu", CommandType.StoredProcedure);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DuLieuDTO dl = new DuLieuDTO();
                    dl.MaDuLieu = dt.Rows[i]["MaDuLieu"].ToString();
                    dl.TenDuLieu = dt.Rows[i]["TenDuLieu"].ToString();
                    dl.SoNha = dt.Rows[i]["SoNha"].ToString();
                    dl.TenDuong = dt.Rows[i]["TenDuong"].ToString();
                    dl.MaPhuong = dt.Rows[i]["TenPhuong"].ToString();
                    dl.MaQuanHuyen = dt.Rows[i]["TenQuanHuyen"].ToString();
                    dl.MaTinhThanh = dt.Rows[i]["TenTinhThanh"].ToString();
                    dl.MaDichVu = dt.Rows[i]["TenDichVu"].ToString();
                    dl.KinhDo = float.Parse(dt.Rows[i]["KinhDo"].ToString());
                    dl.ViDo = float.Parse(dt.Rows[i]["ViDo"].ToString());
                    lst.Add(dl);
                }
            }
            catch (Exception ex)
            {

            }
            return lst;
        }
        #endregion

        #region 2. Lay Thong Tin Theo Quan Huyen
        public static List<DuLieuDTO> getDataByID(String maQuanHuyen)
        {
            List<DuLieuDTO> list = new List<DuLieuDTO>();
            try
            {
                SQLConnect cnn = new SQLConnect();
                DataTable dt = new DataTable();
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@maQuanHuyen", maQuanHuyen));
                dt = cnn.ExecuteQuery("sp_layThongTinDuLieuTheoQuanHuyen", CommandType.StoredProcedure, sqlPara);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DuLieuDTO dl = new DuLieuDTO();
                    dl.MaDuLieu = dt.Rows[i]["MaDuLieu"].ToString();
                    dl.TenDuLieu = dt.Rows[i]["TenDuLieu"].ToString();
                    dl.SoNha = dt.Rows[i]["SoNha"].ToString();
                    dl.TenDuong = dt.Rows[i]["TenDuong"].ToString();
                    dl.MaPhuong = dt.Rows[i]["TenPhuong"].ToString();
                    dl.MaQuanHuyen = dt.Rows[i]["TenQuanHuyen"].ToString();
                    dl.MaTinhThanh = dt.Rows[i]["TenTinhThanh"].ToString();
                    dl.MaDichVu = dt.Rows[i]["TenDichVu"].ToString();
                    dl.KinhDo = float.Parse(dt.Rows[i]["KinhDo"].ToString());
                    dl.ViDo = float.Parse(dt.Rows[i]["ViDo"].ToString());
                    list.Add(dl);
                }
            }
            catch(Exception ex){
                throw ex;
            }
            return list;
        }

        #endregion

        #region 3. Insert
        public static bool Insert(DuLieuDTO dl)
        {
            bool result = false;
            try
            {
                SQLConnect cnn = new SQLConnect();
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@maDuLieu", dl.MaDuLieu));
                sqlPara.Add(new SqlParameter("@tenDuLieu", dl.TenDuLieu));
                sqlPara.Add(new SqlParameter("@soNha", dl.SoNha));
                sqlPara.Add(new SqlParameter("@tenDuong", dl.TenDuong));
                sqlPara.Add(new SqlParameter("@maPhuong", dl.MaPhuong));
                sqlPara.Add(new SqlParameter("@maQuanHuyen", dl.MaQuanHuyen));
                sqlPara.Add(new SqlParameter("@maTinhThanh", dl.MaTinhThanh));
                sqlPara.Add(new SqlParameter("@maDichVu", dl.MaDichVu));
                sqlPara.Add(new SqlParameter("@maKinhDo", dl.KinhDo));
                sqlPara.Add(new SqlParameter("@maViDo", dl.ViDo));
                sqlPara.Add(new SqlParameter("@maTaiKhoan", dl.MaTaiKhoan));

                int n = cnn.ExecuteNonQuery("sp_ThemThongTinDichVu", CommandType.StoredProcedure, sqlPara);
                if (n == 1)
                {
                    result = true;
                }

            }
            catch (Exception ex) { }
            return result;
        }
        #endregion
    }
}